package com.zutassociation.web.controller.system;

import com.zutassociation.common.core.controller.BaseController;
import com.zutassociation.common.core.domain.AjaxResult;
import com.zutassociation.common.core.page.TableDataInfo;
import com.zutassociation.common.utils.SecurityUtils;
import com.zutassociation.system.domain.Forum;
import com.zutassociation.system.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 论坛 信息操作处理
 *
 * @author 黄玉东
 * @version 1.0
 * @date 2021/4/20 21:13
 */
@RestController
@RequestMapping("/forum")
public class ForumController extends BaseController {

    @Autowired
    private ForumService forumService;

    /**
     * 获取论坛列表
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:list')")
    @GetMapping("/list")
    public TableDataInfo list(Forum forum) {
        startPage();
        List<Forum> list = forumService.selectForumList(forum);
        return getDataTable(list);
    }

    /**
     * 根据发送人论坛编号获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:query')")
    @GetMapping(value = "/forum/{userId}")
    public AjaxResult getFromInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success(forumService.selectForumByUserId(userId));
    }

    /**
     * 根据论坛编号获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:query')")
    @GetMapping(value = "/{forumId}")
    public AjaxResult getInfo(@PathVariable("forumId") Long forumId) {
        return AjaxResult.success(forumService.selectForumById(forumId));
    }

    /**
     * 新增论坛
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Forum forum) {
        forum.setCreateBy(SecurityUtils.getUsername());
        AjaxResult ajax = AjaxResult.success();
        int row =forumService.insertForum(forum);
        if (row>0){
            ajax.put("forumId",forum.getForumId());
            return ajax;
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改论坛
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Forum forum) {
        forum.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(forumService.updateForum(forum));
    }

    /**
     * 修改论坛状态
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@Validated @RequestBody Forum forum) {
        forum.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(forumService.updateForumStatus(forum));
    }

    /**
     * 修改论坛举报状态
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateReport")
    public AjaxResult updateReport(@Validated @RequestBody Forum forum) {
        forum.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(forumService.updateForumReport(forum));
    }

    /**
     * 修改论坛结帖状态
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateAccept")
    public AjaxResult updateAccept(@Validated @RequestBody Forum forum) {
        forum.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(forumService.updateForumAccept(forum));
    }
    /**
     * 浏览量+1
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateClick")
    public AjaxResult updateClick(@Validated @RequestBody Forum forum) {
        return toAjax(forumService.updateForumClick(forum));
    }
    /**
     * 点赞量+1
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateFabulous")
    public AjaxResult updateFabulous(@Validated @RequestBody Forum forum) {
        return toAjax(forumService.updateForumFabulous(forum));
    }

    /**
     * 收藏量+1
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateCollection")
    public AjaxResult updateCollection(@Validated @RequestBody Forum forum) {
        return toAjax(forumService.updateForumCollection(forum));
    }
    /**
     * 回复量+1
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:edit')")
    @PutMapping("/updateReply")
    public AjaxResult updateReply(@Validated @RequestBody Forum forum) {
        return toAjax(forumService.updateForumReply(forum));
    }
    @PutMapping("/updateReplyDel")
    public AjaxResult updateReplydel(@Validated @RequestBody Forum forum) {
        return toAjax(forumService.updateForumReplyDel(forum));
    }

    /**
     * 删除论坛
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:remove')")
    @DeleteMapping("/{forumIds}")
    public AjaxResult remove(@PathVariable Long[] forumIds) {
        return toAjax(forumService.deleteForumByIds(forumIds));
    }

    /**
     * 批量修改论坛举报状态为 1 成功
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:remove')")
    @DeleteMapping("/delReport1/{forumIds}")
    public AjaxResult deleteReport1(@PathVariable Long[] forumIds) {
        return toAjax(forumService.deleteForumByReport1(forumIds));
    }

    /**
     * 批量修改论坛举报状态为 2 不成功
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:remove')")
    @DeleteMapping("/delReport2/{forumIds}")
    public AjaxResult deleteReport2(@PathVariable Long[] forumIds) {
        return toAjax(forumService.deleteForumByReport2(forumIds));
    }

    /**
     * 批量修改论坛结帖状态为 0
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:remove')")
    @DeleteMapping("/delAccept0/{forumIds}")
    public AjaxResult deleteAccept0(@PathVariable Long[] forumIds) {
        return toAjax(forumService.deleteForumByAccept0(forumIds));
    }

    /**
     * 批量修改论坛结帖状态为 1
     */
//    @PreAuthorize("@ss.hasPermi('system:forum:remove')")
    @DeleteMapping("/delAccept1/{forumIds}")
    public AjaxResult deleteAccept1(@PathVariable Long[] forumIds) {
        return toAjax(forumService.deleteForumByAccept1(forumIds));
    }

}
